package com.alexsun.newslist

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView

class NewsArrayAdapter(context: Context, private val RESOURCE_ID: Int, objects: List<NewsItem>) :
    ArrayAdapter<NewsItem>(context, RESOURCE_ID, objects) {

    internal class ViewHolder(var itemView: View) {
        var image: ImageView = itemView.findViewById(R.id.item_image_view)
        var textView: TextView = itemView.findViewById(R.id.item_text_view)
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val itemView: View
        val holder: ViewHolder
        if (convertView == null) {
            itemView = LayoutInflater.from(parent.context).inflate(RESOURCE_ID, parent, false)
            holder = ViewHolder(itemView)
            itemView.tag = holder
        } else {
            itemView = convertView
            holder = itemView.tag as ViewHolder
        }
        val data: NewsItem? = getItem(position)
        holder.textView.text = data?.getTitle()

        return itemView
    }
}